package com.aqiang.day0421.view.activity;

import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.RadioButton;

import com.aqiang.core.mvp.view.BaseActivity;
import com.aqiang.day0421.R;
import com.aqiang.day0421.contarct.MainContract;
import com.aqiang.day0421.view.fagment.FourFragment;
import com.aqiang.day0421.view.fagment.OneFragment;
import com.aqiang.day0421.view.fagment.ThreeFragment;
import com.aqiang.day0421.view.fagment.TwoFragment;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends BaseActivity implements MainContract.MainView {

    @BindView(R.id.act_main_r1)
    RadioButton actMainR1;
    @BindView(R.id.act_main_r2)
    RadioButton actMainR2;
    @BindView(R.id.act_main_r3)
    RadioButton actMainR3;
    @BindView(R.id.act_main_r4)
    RadioButton actMainR4;
    private int index = 0;
    //fragment缓存
    private HashMap<Integer, Fragment> fragmentHashMap;
    @Override
    protected int bindLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    protected void initData() {
        fragmentHashMap = new HashMap<>();
        select(actMainR1.getId());
    }

    @Override
    protected void initEvent() {

    }


    @OnClick({R.id.act_main_r1, R.id.act_main_r2, R.id.act_main_r3, R.id.act_main_r4})
    public void onViewClicked(View view) {
        actMainR1.setTextColor(Color.GRAY);
        actMainR2.setTextColor(Color.GRAY);
        actMainR3.setTextColor(Color.GRAY);
        actMainR4.setTextColor(Color.GRAY);
        switch (view.getId()) {
            case R.id.act_main_r1:
                select(actMainR1.getId());
                actMainR1.setTextColor(Color.BLACK);
                break;
            case R.id.act_main_r2:
                select(actMainR2.getId());
                actMainR2.setTextColor(Color.BLACK);
                break;
            case R.id.act_main_r3:
                select(actMainR3.getId());
                actMainR3.setTextColor(Color.BLACK);
                break;
            case R.id.act_main_r4:
                select(actMainR4.getId());
                actMainR4.setTextColor(Color.BLACK);
                break;
        }
    }

    private void select(int id) {
        if(index == id){
            return;
        }
        showFragment(id);
        index = id;
    }


    @Override
    public void showFragment(int id) {
        if(!fragmentHashMap.isEmpty()){
            getSupportFragmentManager().beginTransaction().hide(fragmentHashMap.get(index)).commit();
        }
        if(fragmentHashMap.containsKey(id)){
            getSupportFragmentManager().beginTransaction().show(fragmentHashMap.get(id)).commit();
        }else {
             switch(id){
                 case R.id.act_main_r1:
                     fragmentHashMap.put(id,new OneFragment());
                     break;
                 case R.id.act_main_r2:
                     fragmentHashMap.put(id,new TwoFragment());
                     break;
                 case R.id.act_main_r3:
                     fragmentHashMap.put(id,new ThreeFragment());
                     break;
                 case R.id.act_main_r4:
                     fragmentHashMap.put(id,new FourFragment());
                     break;
                 default:

                     break;
             }
             getSupportFragmentManager().beginTransaction().add(R.id.frag,fragmentHashMap.get(id)).commit();
        }
    }
}
